package com.villaday.idf.activity;

import android.content.Intent;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter.UpgradeVipGitPacgageAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.DividerGridItem;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PreferencesUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 升级成为VIP
 */
public class UpgradeVipActivity extends BaseActivity implements OnAdapterCallbackListener, SwipeRefreshLayout.OnRefreshListener
        , OkHttpUtil.OnDataListener, AbsRecyclerViewAdapter.OnItemClickListener {

    @BindView(R.id.toolbar_text)
    public Toolbar toolbar;

    @BindView(R.id.rv_firstCenter)
    public RecyclerView recyclerView;
    @BindView(R.id.srl)
    public SwipeRefreshLayout swipeRefreshLayout;

    private UpgradeVipGitPacgageAdapter recyclerViewAdapter;

    private Gson gson = new GsonBuilder().create();
    private int pageIndex = 0;
    private int nextPage = 0;
    private int pageSize = 10;
    private String userId;
    private LoadingDialog loadingDialog;
    private List<Integer> dataList = new ArrayList<>();


    @Override
    protected int getViewResId() {
        return R.layout.activity_upgrade_vip;
    }

    @Override
    protected void init() {
        userId = PreferencesUtil.getUserId(this);
        ToolBarUtil.initToolBar(toolbar, "升级成为VIP", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));  //设置图片宽高
        imageView.setImageResource(R.drawable.bg_vip_git_package); //图片资源
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewAdapter = new UpgradeVipGitPacgageAdapter(this, this);
        recyclerViewAdapter.addHeaderView(imageView);
        recyclerViewAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerView.addItemDecoration(new DividerGridItem(this, DividerGridItem.ATTRS));
/*        recyclerViewAdapter.setEmptyView(R.layout.empty_tips);
        recyclerViewAdapter.setEmptyTips(R.id.tv_emptyTips, getString(R.string.emptyAddress));*/
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        dataList.add(0);
        dataList.add(0);
        recyclerViewAdapter.setData(dataList);
    }


    @Override
    protected void loadData() {
        pageIndex = 1;
        nextPage = 1;
/*        loadingDialog = LoadingDialog.newInstance("订单加载...");
        loadingDialog.show(getFragmentManager());*/
        getData();
    }


    private void getData() {
/*        String toJson = gson.toJson(new TransferRecored(userId, "1", pageSize + "", pageIndex + ""));
        OkHttpUtil.postJson(Constant.URL.GetUserTransferBalancePageList, DesUtil.encrypt(toJson), this);*/
    }


    @Override
    public void onCallback() {
        if (nextPage == pageIndex + 1) {
            pageIndex++;
            getData();
        }
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    @Override
    public void onResponse(String url, String json) {
/*        dismissLoading();
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.GetUserTransferBalancePageList.equals(url)) {
                String decrypt = DesUtil.decrypt(json);
                LogUtils.e("loge", "余额转出记录：" + decrypt);
                TransferRecoredEntity getUserOrderByStatusEntity = gson.fromJson(decrypt, TransferRecoredEntity.class);
                if (getUserOrderByStatusEntity.getCode() == Constant.Integers.SUC) {
                    dismissLoading();
                    if (pageIndex == 1 && nextPage == 1) {
                        if (dataList == null) {
                            dataList = new ArrayList<>();
                        } else {
                            dataList.clear();
                        }

                    }
                    removeLoadingItem();
                    dataList.addAll(getUserOrderByStatusEntity.getData().getRows());
                    if (dataList.size() % pageSize == 0 &&
                            dataList.size() != 0) {//可能还有下一页
                        if (dataList.size() >= pageSize) {
                            dataList.add(new TransferRecoredEntity.DataBean.RowsBean(1));
                            nextPage = pageIndex + 1;
                        }

                    } else {
                        if (pageIndex > 0)
                            addBaseLine();
                    }
                } else {
                    removeLoadingItem();
                    addBaseLine();
                }
                recyclerViewAdapter.setData(dataList);
            }
        }*/
    }

    private void addBaseLine() {
/*        if (pageIndex != 1) {
            dataList.add(new TransferRecoredEntity.DataBean.RowsBean(2));
        }*/
    }

    private void removeLoadingItem() {
/*        if (dataList != null) {
            if (dataList.size() > 0) {
                if (dataList.get(dataList.size() - 1).getType() == 1) {
                    dataList.remove(dataList.size() - 1);
                }
            }
        }*/
    }


    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {
        if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onItemClick(View v, int position) {
        switch (v.getId()) {
            case R.id.tv_upgrade_btn: {
                break;
            }

            case R.id.ll_inner_item_layout: {
                startActivity(new Intent(this,UpgradeVipPaymentActivity.class));
                break;
            }
        }
    }
}
